home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / sprite / gdb / infrun.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-30  |  52.8 KB  |  1,797 lines

  1. /* Start (run) and stop the inferior process, for GDB.
  2.    Copyright (C) 1986, 1987, 1988, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Notes on the algorithm used in wait_for_inferior to determine if we
  21.    just did a subroutine call when stepping.  We have the following
  22.    information at that point:
  23.  
  24.                   Current and previous (just before this step) pc.
  25.           Current and previous sp.
  26.           Current and previous start of current function.
  27.  
  28.    If the start's of the functions don't match, then
  29.  
  30.        a) We did a subroutine call.
  31.  
  32.    In this case, the pc will be at the beginning of a function.
  33.  
  34.     b) We did a subroutine return.
  35.  
  36.    Otherwise.
  37.  
  38.     c) We did a longjmp.
  39.  
  40.    If we did a longjump, we were doing "nexti", since a next would
  41.    have attempted to skip over the assembly language routine in which
  42.    the longjmp is coded and would have simply been the equivalent of a
  43.    continue.  I consider this ok behaivior.  We'd like one of two
  44.    things to happen if we are doing a nexti through the longjmp()
  45.    routine: 1) It behaves as a stepi, or 2) It acts like a continue as
  46.    above.  Given that this is a special case, and that anybody who
  47.    thinks that the concept of sub calls is meaningful in the context
  48.    of a longjmp, I'll take either one.  Let's see what happens.  
  49.  
  50.    Acts like a subroutine return.  I can handle that with no problem
  51.    at all.
  52.  
  53.    -->So: If the current and previous beginnings of the current
  54.    function don't match, *and* the pc is at the start of a function,
  55.    we've done a subroutine call.  If the pc is not at the start of a
  56.    function, we *didn't* do a subroutine call.  
  57.  
  58.    -->If the beginnings of the current and previous function do match,
  59.    either: 
  60.  
  61.        a) We just did a recursive call.
  62.  
  63.        In this case, we would be at the very beginning of a
  64.        function and 1) it will have a prologue (don't jump to
  65.        before prologue, or 2) (we assume here that it doesn't have
  66.        a prologue) there will have been a change in the stack
  67.        pointer over the last instruction.  (Ie. it's got to put
  68.        the saved pc somewhere.  The stack is the usual place.  In
  69.        a recursive call a register is only an option if there's a
  70.        prologue to do something with it.  This is even true on
  71.        register window machines; the prologue sets up the new
  72.        window.  It might not be true on a register window machine
  73.        where the call instruction moved the register window
  74.        itself.  Hmmm.  One would hope that the stack pointer would
  75.        also change.  If it doesn't, somebody send me a note, and
  76.        I'll work out a more general theory.
  77.        bug-gdb@prep.ai.mit.edu).  This is true (albeit slipperly
  78.        so) on all machines I'm aware of:
  79.  
  80.           m68k:    Call changes stack pointer.  Regular jumps don't.
  81.  
  82.           sparc:    Recursive calls must have frames and therefor,
  83.                     prologues.
  84.  
  85.           vax:    All calls have frames and hence change the
  86.                     stack pointer.
  87.  
  88.     b) We did a return from a recursive call.  I don't see that we
  89.        have either the ability or the need to distinguish this
  90.        from an ordinary jump.  The stack frame will be printed
  91.        when and if the frame pointer changes; if we are in a
  92.        function without a frame pointer, it's the users own
  93.        lookout.
  94.  
  95.     c) We did a jump within a function.  We assume that this is
  96.        true if we didn't do a recursive call.
  97.  
  98.     d) We are in no-man's land ("I see no symbols here").  We
  99.        don't worry about this; it will make calls look like simple
  100.        jumps (and the stack frames will be printed when the frame
  101.        pointer moves), which is a reasonably non-violent response.
  102.  
  103. #if 0
  104.     We skip this; it causes more problems than it's worth.
  105. #ifdef SUN4_COMPILER_FEATURE
  106.     We do a special ifdef for the sun 4, forcing it to single step
  107.   into calls which don't have prologues.  This means that we can't
  108.   nexti over leaf nodes, we can probably next over them (since they
  109.   won't have debugging symbols, usually), and we can next out of
  110.   functions returning structures (with a "call .stret4" at the end).
  111. #endif
  112. #endif
  113. */
  114.    
  115.  
  116.    
  117.    
  118.  
  119. #include <stdio.h>
  120. #include <string.h>
  121. #include "defs.h"
  122. #include "param.h"
  123. #include "symtab.h"
  124. #include "frame.h"
  125. #include "inferior.h"
  126. #include "breakpoint.h"
  127. #include "wait.h"
  128. #include "gdbcore.h"
  129. #include "signame.h"
  130. #include "command.h"
  131. #include "terminal.h"        /* For #ifdef TIOCGPGRP and new_tty */
  132. #include "target.h"
  133.  
  134. #include <signal.h>
  135.  
  136. /* unistd.h is needed to #define X_OK */
  137. #ifdef USG
  138. #include <unistd.h>
  139. #else
  140. #include <sys/file.h>
  141. #endif
  142.  
  143. #ifdef SET_STACK_LIMIT_HUGE
  144. #include <sys/time.h>
  145. #include <sys/resource.h>
  146.  
  147. extern int original_stack_limit;
  148. #endif /* SET_STACK_LIMIT_HUGE */
  149.  
  150. extern char *getenv ();
  151. extern char **environ;
  152.  
  153. extern struct target_ops child_ops;    /* In inftarg.c */
  154.  
  155.  
  156. /* Sigtramp is a routine that the kernel calls (which then calls the
  157.    signal handler).  On most machines it is a library routine that
  158.    is linked into the executable.
  159.  
  160.    This macro, given a program counter value and the name of the
  161.    function in which that PC resides (which can be null if the
  162.    name is not known), returns nonzero if the PC and name show
  163.    that we are in sigtramp.
  164.  
  165.    On most machines just see if the name is sigtramp (and if we have
  166.    no name, assume we are not in sigtramp).  */
  167. #if !defined (IN_SIGTRAMP)
  168. #define IN_SIGTRAMP(pc, name) \
  169.   name && !strcmp ("_sigtramp", name)
  170. #endif
  171.  
  172. #ifdef TDESC
  173. #include "tdesc.h"
  174. int safe_to_init_tdesc_context = 0;
  175. extern dc_dcontext_t current_context;
  176. #endif
  177.  
  178. /* Tables of how to react to signals; the user sets them.  */
  179.  
  180. static char signal_stop[NSIG];
  181. static char signal_print[NSIG];
  182. static char signal_program[NSIG];
  183.  
  184. /* Nonzero if breakpoints are now inserted in the inferior.  */
  185. /* Nonstatic for initialization during xxx_create_inferior. FIXME. */
  186.  
  187. /*static*/ int breakpoints_inserted;
  188.  
  189. /* Function inferior was in as of last step command.  */
  190.  
  191. static struct symbol *step_start_function;
  192.  
  193. /* Nonzero => address for special breakpoint for resuming stepping.  */
  194.  
  195. static CORE_ADDR step_resume_break_address;
  196.  
  197. /* Pointer to orig contents of the byte where the special breakpoint is.  */
  198.  
  199. static char step_resume_break_shadow[BREAKPOINT_MAX];
  200.  
  201. /* Nonzero means the special breakpoint is a duplicate
  202.    so it has not itself been inserted.  */
  203.  
  204. static int step_resume_break_duplicate;
  205.  
  206. /* Nonzero if we are expecting a trace trap and should proceed from it.  */
  207.  
  208. static int trap_expected;
  209.  
  210. /* Nonzero if the next time we try to continue the inferior, it will
  211.    step one instruction and generate a spurious trace trap.
  212.    This is used to compensate for a bug in HP-UX.  */
  213.  
  214. static int trap_expected_after_continue;
  215.  
  216. /* Nonzero means expecting a trace trap
  217.    and should stop the inferior and return silently when it happens.  */
  218.  
  219. int stop_after_trap;
  220.  
  221. /* Nonzero means expecting a trap and caller will handle it themselves.
  222.    It is used after attach, due to attaching to a process;
  223.    when running in the shell before the child program has been exec'd;
  224.    and when running some kinds of remote stuff (FIXME?).  */
  225.  
  226. int stop_soon_quietly;
  227.  
  228. /* Nonzero if pc has been changed by the debugger
  229.    since the inferior stopped.  */
  230.  
  231. int pc_changed;
  232.  
  233. /* Nonzero if proceed is being used for a "finish" command or a similar
  234.    situation when stop_registers should be saved.  */
  235.  
  236. int proceed_to_finish;
  237.  
  238. /* Save register contents here when about to pop a stack dummy frame,
  239.    if-and-only-if proceed_to_finish is set.
  240.    Thus this contains the return value from the called function (assuming
  241.    values are returned in a register).  */
  242.  
  243. char stop_registers[REGISTER_BYTES];
  244.  
  245. /* Nonzero if program stopped due to error trying to insert breakpoints.  */
  246.  
  247. static int breakpoints_failed;
  248.  
  249. /* Nonzero after stop if current stack frame should be printed.  */
  250.  
  251. static int stop_print_frame;
  252.  
  253. #ifdef NO_SINGLE_STEP
  254. extern int one_stepped;        /* From machine dependent code */
  255. extern void single_step ();    /* Same. */
  256. #endif /* NO_SINGLE_STEP */
  257.  
  258. static void insert_step_breakpoint ();
  259. static void remove_step_breakpoint ();
  260. /*static*/ void wait_for_inferior ();
  261. void init_wait_for_inferior ();
  262. void normal_stop ();
  263.  
  264.  
  265. /* Things to clean up if we QUIT out of resume ().  */
  266. /* ARGSUSED */
  267. static void
  268. resume_cleanups (arg)
  269.      int arg;
  270. {
  271.   normal_stop ();
  272. }
  273.  
  274. /* Resume the inferior, but allow a QUIT.  This is useful if the user
  275.    wants to interrupt some lengthy single-stepping operation
  276.    (for child processes, the SIGINT goes to the inferior, and so
  277.    we get a SIGINT random_signal, but for remote debugging and perhaps
  278.    other targets, that's not true).
  279.  
  280.    STEP nonzero if we should step (zero to continue instead).
  281.    SIG is the signal to give the inferior (zero for none).  */
  282. static void
  283. resume (step, sig)
  284.      int step;
  285.      int sig;
  286. {
  287.   struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
  288.   QUIT;
  289.  
  290. #ifdef NO_SINGLE_STEP
  291.   if (step) {
  292.     single_step();    /* Do it the hard way, w/temp breakpoints */
  293.     step = 0;        /* ...and don't ask hardware to do it.  */
  294.   }
  295. #endif
  296.  
  297.   /* Handle any optimized stores to the inferior NOW...  */
  298. #ifdef DO_DEFERRED_STORES
  299.   DO_DEFERRED_STORES;
  300. #endif
  301.  
  302.   target_resume (step, sig);
  303.   discard_cleanups (old_cleanups);
  304. }
  305.  
  306.  
  307. /* Clear out all variables saying what to do when inferior is continued.
  308.    First do this, then set the ones you want, then call `proceed'.  */
  309.  
  310. void
  311. clear_proceed_status ()
  312. {
  313.   trap_expected = 0;
  314.   step_range_start = 0;
  315.   step_range_end = 0;
  316.   step_frame_address = 0;
  317.   step_over_calls = -1;
  318.   step_resume_break_address = 0;
  319.   stop_after_trap = 0;
  320.   stop_soon_quietly = 0;
  321.   proceed_to_finish = 0;
  322.   breakpoint_proceeded = 1;    /* We're about to proceed... */
  323.  
  324.   /* Discard any remaining commands or status from previous stop.  */
  325.   bpstat_clear (&stop_bpstat);
  326. }
  327.  
  328. /* Basic routine for continuing the program in various fashions.
  329.  
  330.    ADDR is the address to resume at, or -1 for resume where stopped.
  331.    SIGGNAL is the signal to give it, or 0 for none,
  332.      or -1 for act according to how it stopped.
  333.    STEP is nonzero if should trap after one instruction.
  334.      -1 means return after that and print nothing.
  335.      You should probably set various step_... variables
  336.      before calling here, if you are stepping.
  337.  
  338.    You should call clear_proceed_status before calling proceed.  */
  339.  
  340. void
  341. proceed (addr, siggnal, step)
  342.      CORE_ADDR addr;
  343.      int siggnal;
  344.      int step;
  345. {
  346.   int oneproc = 0;
  347.  
  348.   if (step > 0)
  349.     step_start_function = find_pc_function (read_pc ());
  350.   if (step < 0)
  351.     stop_after_trap = 1;
  352.  
  353.   if (addr == -1)
  354.     {
  355.       /* If there is a breakpoint at the address we will resume at,
  356.      step one instruction before inserting breakpoints
  357.      so that we do not stop right away.  */
  358.  
  359.       if (!pc_changed && breakpoint_here_p (read_pc ()))
  360.     oneproc = 1;
  361.     }
  362.   else
  363.     {
  364.       write_register (PC_REGNUM, addr);
  365. #ifdef NPC_REGNUM
  366.       write_register (NPC_REGNUM, addr + 4);
  367. #ifdef NNPC_REGNUM
  368.       write_register (NNPC_REGNUM, addr + 8);
  369. #endif
  370. #endif
  371.     }
  372.  
  373.   if (trap_expected_after_continue)
  374.     {
  375.       /* If (step == 0), a trap will be automatically generated after
  376.      the first instruction is executed.  Force step one
  377.      instruction to clear this condition.  This should not occur
  378.      if step is nonzero, but it is harmless in that case.  */
  379.       oneproc = 1;
  380.       trap_expected_after_continue = 0;
  381.     }
  382.  
  383.   if (oneproc)
  384.     /* We will get a trace trap after one instruction.
  385.        Continue it automatically and insert breakpoints then.  */
  386.     trap_expected = 1;
  387.   else
  388.     {
  389.       int temp = insert_breakpoints ();
  390.       if (temp)
  391.     {
  392.       print_sys_errmsg ("ptrace", temp);
  393.       error ("Cannot insert breakpoints.\n\
  394. The same program may be running in another process.");
  395.     }
  396.       breakpoints_inserted = 1;
  397.     }
  398.  
  399.   /* Install inferior's terminal modes.  */
  400.   target_terminal_inferior ();
  401.  
  402.   if (siggnal >= 0)
  403.     stop_signal = siggnal;
  404.   /* If this signal should not be seen by program,
  405.      give it zero.  Used for debugging signals.  */
  406.   else if (stop_signal < NSIG && !signal_program[stop_signal])
  407.     stop_signal= 0;
  408.  
  409.   /* Resume inferior.  */
  410.   resume (oneproc || step || bpstat_should_step (), stop_signal);
  411.  
  412.   /* Wait for it to stop (if not standalone)
  413.      and in any case decode why it stopped, and act accordingly.  */
  414.  
  415.   wait_for_inferior ();
  416.   normal_stop ();
  417. }
  418.  
  419. #if 0
  420. /* This might be useful (not sure), but isn't currently used.  See also
  421.    write_pc().  */
  422. /* Writing the inferior pc as a register calls this function
  423.    to inform infrun that the pc has been set in the debugger.  */
  424.  
  425. void
  426. writing_pc (val)
  427.      CORE_ADDR val;
  428. {
  429.   stop_pc = val;
  430.   pc_changed = 1;
  431. }
  432. #endif
  433.  
  434. /* Record the pc and sp of the program the last time it stopped.
  435.    These are just used internally by wait_for_inferior, but need
  436.    to be preserved over calls to it and cleared when the inferior
  437.    is started.  */
  438. static CORE_ADDR prev_pc;
  439. static CORE_ADDR prev_sp;
  440. static CORE_ADDR prev_func_start;
  441. static char *prev_func_name;
  442.  
  443.  
  444. /* Start an inferior Unix child process and sets inferior_pid to its pid.
  445.    EXEC_FILE is the file to run.
  446.    ALLARGS is a string containing the arguments to the program.
  447.    ENV is the environment vector to pass.  Errors reported with error().  */
  448.  
  449. #ifndef SHELL_FILE
  450. #define SHELL_FILE "/bin/sh"
  451. #endif
  452.  
  453. void
  454. child_create_inferior (exec_file, allargs, env)
  455.      char *exec_file;
  456.      char *allargs;
  457.      char **env;
  458. {
  459.   int pid;
  460.   char *shell_command;
  461.   extern int sys_nerr;
  462.   extern char *sys_errlist[];
  463.   char *shell_file;
  464.   static char default_shell_file[] = SHELL_FILE;
  465.   int len;
  466.   int pending_execs;
  467.   /* Set debug_fork then attach to the child while it sleeps, to debug. */
  468.   static int debug_fork = 0;
  469.   /* This is set to the result of setpgrp, which if vforked, will be visible
  470.      to you in the parent process.  It's only used by humans for debugging.  */
  471.   static int debug_setpgrp = 657473;
  472.   char **save_our_env;
  473.  
  474.   /* The user might want tilde-expansion, and in general probably wants
  475.      the program to behave the same way as if run from
  476.      his/her favorite shell.  So we let the shell run it for us.
  477.      FIXME, this should probably search the local environment (as
  478.      modified by the setenv command), not the env gdb inherited.  */
  479.   shell_file = getenv ("SHELL");
  480.   if (shell_file == NULL)
  481.     shell_file = default_shell_file;
  482.   
  483.   len = 5 + 6 + strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 10;
  484.   /* If desired, concat something onto the front of ALLARGS.
  485.      SHELL_COMMAND is the result.  */
  486. #ifdef SHELL_COMMAND_CONCAT
  487.   shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
  488.   strcpy (shell_command, SHELL_COMMAND_CONCAT);
  489. #else
  490.   shell_command = (char *) alloca (len);
  491.   shell_command[0] = '\0';
  492. #endif
  493.   strcat (shell_command, "exec debug ");
  494.   strcat (shell_command, exec_file);
  495.   strcat (shell_command, " ");
  496.   strcat (shell_command, allargs);
  497.  
  498.   /* exec is said to fail if the executable is open.  */
  499.   close_exec_file ();
  500.  
  501.   /* Retain a copy of our environment variables, since the child will
  502.      replace the value of  environ  and if we're vforked, we have to 
  503.      restore it.  */
  504.   save_our_env = environ;
  505.  
  506.   /* Tell the terminal handling subsystem what tty we plan to run on;
  507.      it will just record the information for later.  */
  508.  
  509.   new_tty_prefork (inferior_io_terminal);
  510.  
  511. #if defined(USG) && !defined(HAVE_VFORK)
  512.   pid = fork ();
  513. #else
  514.   if (debug_fork)
  515.     pid = fork ();
  516.   else
  517. #ifdef sprite
  518.     pid = fork ();
  519. #else
  520.     pid = vfork ();
  521. #endif
  522. #endif
  523.  
  524.   if (pid < 0)
  525.     perror_with_name ("vfork");
  526.  
  527.   if (pid == 0)
  528.     {
  529.       if (debug_fork) 
  530.     sleep (debug_fork);
  531.  
  532. #ifdef TIOCGPGRP
  533.       /* Run inferior in a separate process group.  */
  534.       debug_setpgrp = setpgrp (getpid (), getpid ());
  535.       if (0 != debug_setpgrp)
  536.      perror("setpgrp failed in child");
  537. #endif /* TIOCGPGRP */
  538.  
  539. #ifdef SET_STACK_LIMIT_HUGE
  540.       /* Reset the stack limit back to what it was.  */
  541.       {
  542.     struct rlimit rlim;
  543.  
  544.     getrlimit (RLIMIT_STACK, &rlim);
  545.     rlim.rlim_cur = original_stack_limit;
  546.     setrlimit (RLIMIT_STACK, &rlim);
  547.       }
  548. #endif /* SET_STACK_LIMIT_HUGE */
  549.  
  550.       /* Ask the tty subsystem to switch to the one we specified earlier
  551.      (or to share the current terminal, if none was specified).  */
  552.  
  553.       new_tty ();
  554.  
  555.       /* Changing the signal handlers for the inferior after
  556.      a vfork can also change them for the superior, so we don't mess
  557.      with signals here.  See comments in
  558.      initialize_signals for how we get the right signal handlers
  559.      for the inferior.  */
  560.  
  561. #ifdef sprite
  562.       /*
  563.        * This appears to be needed on Sprite.
  564.        */
  565.       signal (SIGQUIT, SIG_DFL);
  566.       signal (SIGINT, SIG_DFL);
  567. #endif
  568.  
  569.       call_ptrace (0, 0, 0, 0);        /* "Trace me, Dr. Memory!" */
  570.  
  571.       /* There is no execlpe call, so we have to set the environment
  572.      for our child in the global variable.  If we've vforked, this
  573.      clobbers the parent, but environ is restored a few lines down
  574.      in the parent.  By the way, yes we do need to look down the
  575.      path to find $SHELL.  Rich Pixley says so, and I agree.  */
  576.       environ = env;
  577.       execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
  578.  
  579.       fprintf (stderr, "Cannot exec %s: %s.\n", shell_file,
  580.            errno < sys_nerr ? sys_errlist[errno] : "unknown error");
  581.       fflush (stderr);
  582.       _exit (0177);
  583.     }
  584.  
  585.   /* Restore our environment in case a vforked child clob'd it.  */
  586.   environ = save_our_env;
  587.  
  588.   /* Now that we have a child process, make it our target.  */
  589.   push_target (&child_ops);
  590.  
  591. #ifdef CREATE_INFERIOR_HOOK
  592.   CREATE_INFERIOR_HOOK (pid);
  593. #endif  
  594.  
  595. /* The process was started by the fork that created it,
  596.    but it will have stopped one instruction after execing the shell.
  597.    Here we must get it up to actual execution of the real program.  */
  598.  
  599.   inferior_pid = pid;        /* Needed for wait_for_inferior stuff below */
  600.  
  601.   clear_proceed_status ();
  602.  
  603. #if defined (START_INFERIOR_HOOK)
  604.   START_INFERIOR_HOOK ();
  605. #endif
  606.  
  607.   /* We will get a trace trap after one instruction.
  608.      Continue it automatically.  Eventually (after shell does an exec)
  609.      it will get another trace trap.  Then insert breakpoints and continue.  */
  610.  
  611. #ifdef START_INFERIOR_TRAPS_EXPECTED
  612.   pending_execs = START_INFERIOR_TRAPS_EXPECTED;
  613. #else
  614.   pending_execs = 2;
  615. #endif
  616.  
  617.   init_wait_for_inferior ();
  618.  
  619.   /* Set up the "saved terminal modes" of the inferior
  620.      based on what modes we are starting it with.  */
  621.   target_terminal_init ();
  622.  
  623.   /* Install inferior's terminal modes.  */
  624.   target_terminal_inferior ();
  625.  
  626.   while (1)
  627.     {
  628.       stop_soon_quietly = 1;    /* Make wait_for_inferior be quiet */
  629.       wait_for_inferior ();
  630.       if (stop_signal != SIGTRAP)
  631.     {
  632.       /* Let shell child handle its own signals in its own way */
  633.       /* FIXME, what if child has exit()ed?  Must exit loop somehow */
  634.       resume (0, stop_signal);
  635.     }
  636.       else
  637.     {
  638.       /* We handle SIGTRAP, however; it means child did an exec.  */
  639.       if (0 == --pending_execs)
  640.         break;
  641.       resume (0, 0);        /* Just make it go on */
  642.     }
  643.     }
  644.   stop_soon_quietly = 0;
  645.  
  646.   /* Should this perhaps just be a "proceed" call?  FIXME */
  647.   insert_step_breakpoint ();
  648.   breakpoints_failed = insert_breakpoints ();
  649.   if (!breakpoints_failed)
  650.     {
  651.       breakpoints_inserted = 1;
  652.       target_terminal_inferior();
  653.       /* Start the child program going on its first instruction, single-
  654.      stepping if we need to.  */
  655.       resume (bpstat_should_step (), 0);
  656.       wait_for_inferior ();
  657.       normal_stop ();
  658.     }
  659. }
  660.  
  661. /* Start remote-debugging of a machine over a serial link.  */
  662.  
  663. void
  664. start_remote ()
  665. {
  666.   init_wait_for_inferior ();
  667.   clear_proceed_status ();
  668.   stop_soon_quietly = 1;
  669.   trap_expected = 0;
  670.   wait_for_inferior ();
  671.   normal_stop ();
  672. }
  673.  
  674. /* Initialize static vars when a new inferior begins.  */
  675.  
  676. void
  677. init_wait_for_inferior ()
  678. {
  679.   /* These are meaningless until the first time through wait_for_inferior.  */
  680.   prev_pc = 0;
  681.   prev_sp = 0;
  682.   prev_func_start = 0;
  683.   prev_func_name = NULL;
  684.  
  685.   trap_expected_after_continue = 0;
  686.   breakpoints_inserted = 0;
  687.   mark_breakpoints_out ();
  688.   stop_signal = 0;        /* Don't confuse first call to proceed(). */
  689. }
  690.  
  691.  
  692. /* Attach to process PID, then initialize for debugging it
  693.    and wait for the trace-trap that results from attaching.  */
  694.  
  695. void
  696. child_attach (args, from_tty)
  697.      char *args;
  698.      int from_tty;
  699. {
  700.   char *exec_file;
  701.   int pid;
  702.  
  703.   dont_repeat();
  704.  
  705.   if (!args)
  706.     error_no_arg ("process-id to attach");
  707.  
  708. #ifndef ATTACH_DETACH
  709.   error ("Can't attach to a process on this machine.");
  710. #else
  711. #ifdef sprite
  712.     /*
  713.      * Since Sprite pids are displayed in hex the atoi doesn't work. We
  714.      * allow the attach command to use any C expression.
  715.      */
  716.     pid = value_as_long (evaluate_expression (parse_c_expression (args)));
  717. #else
  718.   pid = atoi (args);
  719. #endif /* sprite */
  720.  
  721.   if (target_has_execution)
  722.     {
  723.       if (query ("A program is being debugged already.  Kill it? "))
  724.     target_kill ((char *)0, from_tty);
  725.       else
  726.     error ("Inferior not killed.");
  727.     }
  728.  
  729.   exec_file = (char *) get_exec_file (1);
  730.  
  731.   if (from_tty)
  732.     {
  733.       printf ("Attaching program: %s pid %d\n",
  734.           exec_file, pid);
  735.       fflush (stdout);
  736.     }
  737.  
  738.   attach (pid);
  739.   inferior_pid = pid;
  740.   push_target (&child_ops);
  741.  
  742.   mark_breakpoints_out ();
  743.   target_terminal_init ();
  744.   clear_proceed_status ();
  745.   stop_soon_quietly = 1;
  746.   /*proceed (-1, 0, -2);*/
  747.   target_terminal_inferior ();
  748.   wait_for_inferior ();
  749.   normal_stop ();
  750. #endif  /* ATTACH_DETACH */
  751. }
  752.  
  753. /* Wait for control to return from inferior to debugger.
  754.    If inferior gets a signal, we may decide to start it up again
  755.    instead of returning.  That is why there is a loop in this function.
  756.    When this function actually returns it means the inferior
  757.    should be left stopped and GDB should read more commands.  */
  758.  
  759. void
  760. wait_for_inferior ()
  761. {
  762.   WAITTYPE w;
  763.   int another_trap;
  764.   int random_signal;
  765.   CORE_ADDR stop_sp;
  766.   CORE_ADDR stop_func_start;
  767.   char *stop_func_name;
  768.   CORE_ADDR prologue_pc;
  769.   int stop_step_resume_break;
  770.   struct symtab_and_line sal;
  771.   int remove_breakpoints_on_following_step = 0;
  772. #ifdef TDESC
  773.   extern dc_handle_t tdesc_handle;
  774. #endif
  775.  
  776. #if 0
  777.   /* This no longer works now that read_register is lazy;
  778.      it might try to ptrace when the process is not stopped.  */
  779.   prev_pc = read_pc ();
  780.   (void) find_pc_partial_function (prev_pc, &prev_func_name,
  781.                    &prev_func_start);
  782.   prev_func_start += FUNCTION_START_OFFSET;
  783.   prev_sp = read_register (SP_REGNUM);
  784. #endif /* 0 */
  785.  
  786.   while (1)
  787.     {
  788.       /* Clean up saved state that will become invalid.  */
  789.       pc_changed = 0;
  790.       flush_cached_frames ();
  791.       registers_changed ();
  792.  
  793.       target_wait (&w);
  794.  
  795.       /* See if the process still exists; clean up if it doesn't.  */
  796.       if (WIFEXITED (w))
  797.     {
  798.       target_terminal_ours ();    /* Must do this before mourn anyway */
  799. #ifdef TDESC 
  800.           safe_to_init_tdesc_context = 0;
  801. #endif
  802.       if (WEXITSTATUS (w))
  803.         printf ("\nProgram exited with code 0%o.\n", 
  804.              (unsigned int)WEXITSTATUS (w));
  805.       else
  806.         if (!batch_mode())
  807.           printf ("\nProgram exited normally.\n");
  808.       fflush (stdout);
  809.       target_mourn_inferior ();
  810. #ifdef NO_SINGLE_STEP
  811.       one_stepped = 0;
  812. #endif
  813.       stop_print_frame = 0;
  814.       break;
  815.     }
  816.       else if (!WIFSTOPPED (w))
  817.     {
  818.       stop_print_frame = 0;
  819.       stop_signal = WTERMSIG (w);
  820.       target_terminal_ours ();    /* Must do this before mourn anyway */
  821.       target_kill ((char *)0, 0);    /* kill mourns as well */
  822. #ifdef TDESC
  823.           safe_to_init_tdesc_context = 0;
  824. #endif
  825. #ifdef PRINT_RANDOM_SIGNAL
  826.       printf ("\nProgram terminated: ");
  827.       PRINT_RANDOM_SIGNAL (stop_signal);
  828. #else
  829.       printf ("\nProgram terminated with signal %d, %s\n",
  830.           stop_signal,
  831.           stop_signal < NSIG
  832.           ? sys_siglist[stop_signal]
  833.           : "(undocumented)");
  834. #endif
  835.       printf ("The inferior process no longer exists.\n");
  836.       fflush (stdout);
  837. #ifdef NO_SINGLE_STEP
  838.       one_stepped = 0;
  839. #endif
  840.       break;
  841.     }
  842.       
  843. #ifdef NO_SINGLE_STEP
  844.       if (one_stepped)
  845.     single_step (0);    /* This actually cleans up the ss */
  846. #endif /* NO_SINGLE_STEP */
  847.       
  848.       stop_pc = read_pc ();
  849. #ifdef TDESC
  850.       if (safe_to_init_tdesc_context)   
  851.         {
  852.       current_context = init_dcontext();
  853.           set_current_frame ( create_new_frame (get_frame_base (read_pc()),read_pc()));
  854.         }
  855.       else
  856. #endif /* TDESC */
  857.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  858.                         read_pc ()));
  859.       
  860.       stop_frame_address = FRAME_FP (get_current_frame ());
  861.       stop_sp = read_register (SP_REGNUM);
  862.       stop_func_start = 0;
  863.       stop_func_name = 0;
  864.       /* Don't care about return value; stop_func_start and stop_func_name
  865.      will both be 0 if it doesn't work.  */
  866.       (void) find_pc_partial_function (stop_pc, &stop_func_name,
  867.                        &stop_func_start);
  868.       stop_func_start += FUNCTION_START_OFFSET;
  869.       another_trap = 0;
  870.       bpstat_clear (&stop_bpstat);
  871.       stop_step = 0;
  872.       stop_stack_dummy = 0;
  873.       stop_print_frame = 1;
  874.       stop_step_resume_break = 0;
  875.       random_signal = 0;
  876.       stopped_by_random_signal = 0;
  877.       breakpoints_failed = 0;
  878.       
  879.       /* Look at the cause of the stop, and decide what to do.
  880.      The alternatives are:
  881.      1) break; to really stop and return to the debugger,
  882.      2) drop through to start up again
  883.      (set another_trap to 1 to single step once)
  884.      3) set random_signal to 1, and the decision between 1 and 2
  885.      will be made according to the signal handling tables.  */
  886.       
  887.       stop_signal = WSTOPSIG (w);
  888.       
  889.       /* First, distinguish signals caused by the debugger from signals
  890.      that have to do with the program's own actions.
  891.      Note that breakpoint insns may cause SIGTRAP or SIGILL
  892.      or SIGEMT, depending on the operating system version.
  893.      Here we detect when a SIGILL or SIGEMT is really a breakpoint
  894.      and change it to SIGTRAP.  */
  895.       
  896.       if (stop_signal == SIGTRAP
  897.       || (breakpoints_inserted &&
  898.           (stop_signal == SIGILL
  899.            || stop_signal == SIGEMT))
  900.       || stop_soon_quietly)
  901.     {
  902.       if (stop_signal == SIGTRAP && stop_after_trap)
  903.         {
  904.           stop_print_frame = 0;
  905.           break;
  906.         }
  907.       if (stop_soon_quietly)
  908.         break;
  909.  
  910.       /* Don't even think about breakpoints
  911.          if just proceeded over a breakpoint.
  912.  
  913.          However, if we are trying to proceed over a breakpoint
  914.          and end up in sigtramp, then step_resume_break_address
  915.          will be set and we should check whether we've hit the
  916.          step breakpoint.  */
  917.       if (stop_signal == SIGTRAP && trap_expected
  918.           && step_resume_break_address == NULL)
  919.         bpstat_clear (&stop_bpstat);
  920.       else
  921.         {
  922.           /* See if there is a breakpoint at the current PC.  */
  923. #if DECR_PC_AFTER_BREAK
  924.           /* Notice the case of stepping through a jump
  925.          that leads just after a breakpoint.
  926.          Don't confuse that with hitting the breakpoint.
  927.          What we check for is that 1) stepping is going on
  928.          and 2) the pc before the last insn does not match
  929.          the address of the breakpoint before the current pc.  */
  930.           if (!(prev_pc != stop_pc - DECR_PC_AFTER_BREAK
  931.             && step_range_end && !step_resume_break_address))
  932. #endif /* DECR_PC_AFTER_BREAK not zero */
  933.         {
  934.           /* See if we stopped at the special breakpoint for
  935.              stepping over a subroutine call.  If both are zero,
  936.              this wasn't the reason for the stop.  */
  937.           if (stop_pc - DECR_PC_AFTER_BREAK
  938.                         == step_resume_break_address
  939.               && step_resume_break_address)
  940.             {
  941.               stop_step_resume_break = 1;
  942.               if (DECR_PC_AFTER_BREAK)
  943.             {
  944.               stop_pc -= DECR_PC_AFTER_BREAK;
  945.               write_register (PC_REGNUM, stop_pc);
  946.               pc_changed = 0;
  947.             }
  948.             }
  949.           else
  950.             {
  951.               stop_bpstat =
  952.             bpstat_stop_status (&stop_pc, stop_frame_address);
  953.               /* Following in case break condition called a
  954.              function.  */
  955.               stop_print_frame = 1;
  956.             }
  957.         }
  958.         }
  959.       
  960.       if (stop_signal == SIGTRAP)
  961.         random_signal
  962.           = !(bpstat_explains_signal (stop_bpstat)
  963.           || trap_expected
  964.           || stop_step_resume_break
  965.           || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
  966.           || (step_range_end && !step_resume_break_address));
  967.       else
  968.         {
  969.           random_signal
  970.         = !(bpstat_explains_signal (stop_bpstat)
  971.             || stop_step_resume_break
  972.             /* End of a stack dummy.  Some systems (e.g. Sony
  973.                news) give another signal besides SIGTRAP,
  974.                so check here as well as above.  */
  975.             || (stop_sp INNER_THAN stop_pc
  976.             && stop_pc INNER_THAN stop_frame_address)
  977.             );
  978.           if (!random_signal)
  979.         stop_signal = SIGTRAP;
  980.         }
  981.     }
  982.       else
  983.     random_signal = 1;
  984.       
  985.       /* For the program's own signals, act according to
  986.      the signal handling tables.  */
  987.       
  988.       if (random_signal)
  989.     {
  990.       /* Signal not for debugging purposes.  */
  991.       int printed = 0;
  992.       
  993.       stopped_by_random_signal = 1;
  994.       
  995.       if (stop_signal >= NSIG
  996.           || signal_print[stop_signal])
  997.         {
  998.           printed = 1;
  999.           target_terminal_ours_for_output ();
  1000. #ifdef PRINT_RANDOM_SIGNAL
  1001.           PRINT_RANDOM_SIGNAL (stop_signal);
  1002. #else
  1003.           printf ("\nProgram received signal %d, %s\n",
  1004.               stop_signal,
  1005.               stop_signal < NSIG
  1006.               ? sys_siglist[stop_signal]
  1007.               : "(undocumented)");
  1008. #endif /* PRINT_RANDOM_SIGNAL */
  1009.           fflush (stdout);
  1010.         }
  1011.       if (stop_signal >= NSIG
  1012.           || signal_stop[stop_signal])
  1013.         break;
  1014.       /* If not going to stop, give terminal back
  1015.          if we took it away.  */
  1016.       else if (printed)
  1017.         target_terminal_inferior ();
  1018.     }
  1019.       
  1020.       /* Handle cases caused by hitting a breakpoint.  */
  1021.       
  1022.       if (!random_signal
  1023.       && (bpstat_explains_signal (stop_bpstat) || stop_step_resume_break))
  1024.     {
  1025.       /* Does a breakpoint want us to stop?  */
  1026.       if (bpstat_stop (stop_bpstat))
  1027.         {
  1028.           stop_print_frame = bpstat_should_print (stop_bpstat);
  1029.           break;
  1030.         }
  1031.       /* But if we have hit the step-resumption breakpoint,
  1032.          remove it.  It has done its job getting us here.
  1033.          The sp test is to make sure that we don't get hung
  1034.          up in recursive calls in functions without frame
  1035.          pointers.  If the stack pointer isn't outside of
  1036.          where the breakpoint was set (within a routine to be
  1037.          stepped over), we're in the middle of a recursive
  1038.          call. Not true for reg window machines (sparc)
  1039.          because the must change frames to call things and
  1040.          the stack pointer doesn't have to change if it
  1041.          the bp was set in a routine without a frame (pc can
  1042.          be stored in some other window).
  1043.          
  1044.          The removal of the sp test is to allow calls to
  1045.          alloca.  Nasty things were happening.  Oh, well,
  1046.          gdb can only handle one level deep of lack of
  1047.          frame pointer. */
  1048.       if (stop_step_resume_break
  1049.           && (step_frame_address == 0
  1050.           || (stop_frame_address == step_frame_address)))
  1051.         {
  1052.           remove_step_breakpoint ();
  1053.           step_resume_break_address = 0;
  1054.  
  1055.           /* If were waiting for a trap, hitting the step_resume_break
  1056.          doesn't count as getting it.  */
  1057.           if (trap_expected)
  1058.         another_trap = 1;
  1059.         }
  1060.       /* Otherwise, must remove breakpoints and single-step
  1061.          to get us past the one we hit.  */
  1062.       else
  1063.         {
  1064.           remove_breakpoints ();
  1065.           remove_step_breakpoint ();
  1066.           breakpoints_inserted = 0;
  1067.           another_trap = 1;
  1068.         }
  1069.       
  1070.       /* We come here if we hit a breakpoint but should not
  1071.          stop for it.  Possibly we also were stepping
  1072.          and should stop for that.  So fall through and
  1073.          test for stepping.  But, if not stepping,
  1074.          do not stop.  */
  1075.     }
  1076.       
  1077.       /* If this is the breakpoint at the end of a stack dummy,
  1078.      just stop silently.  */
  1079.       if (PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address))
  1080.       {
  1081.         stop_print_frame = 0;
  1082.         stop_stack_dummy = 1;
  1083. #ifdef HP_OS_BUG
  1084.         trap_expected_after_continue = 1;
  1085. #endif
  1086.         break;
  1087.       }
  1088.       
  1089.       if (step_resume_break_address)
  1090.     /* Having a step-resume breakpoint overrides anything
  1091.        else having to do with stepping commands until
  1092.        that breakpoint is reached.  */
  1093.     ;
  1094.       /* If stepping through a line, keep going if still within it.  */
  1095.       else if (!random_signal
  1096.            && step_range_end
  1097.            && stop_pc >= step_range_start
  1098.            && stop_pc < step_range_end
  1099.            /* The step range might include the start of the
  1100.           function, so if we are at the start of the
  1101.           step range and either the stack or frame pointers
  1102.           just changed, we've stepped outside */
  1103.            && !(stop_pc == step_range_start
  1104.             && stop_frame_address
  1105.             && (stop_sp INNER_THAN prev_sp
  1106.             || stop_frame_address != step_frame_address)))
  1107.     {
  1108. #if 0
  1109.       /* When "next"ing through a function,
  1110.          This causes an extra stop at the end.
  1111.          Is there any reason for this?
  1112.          It's confusing to the user.  */
  1113.       /* Don't step through the return from a function
  1114.          unless that is the first instruction stepped through.  */
  1115.       if (ABOUT_TO_RETURN (stop_pc))
  1116.         {
  1117.           stop_step = 1;
  1118.           break;
  1119.         }
  1120. #endif
  1121.     }
  1122.       
  1123.       /* We stepped out of the stepping range.  See if that was due
  1124.      to a subroutine call that we should proceed to the end of.  */
  1125.       else if (!random_signal && step_range_end)
  1126.     {
  1127.       if (stop_func_start)
  1128.         {
  1129.           prologue_pc = stop_func_start;
  1130.           SKIP_PROLOGUE (prologue_pc);
  1131.         }
  1132.  
  1133.       /* Did we just take a signal?  */
  1134.       if (IN_SIGTRAMP (stop_pc, stop_func_name)
  1135.           && !IN_SIGTRAMP (prev_pc, prev_func_name))
  1136.         {
  1137.           /* This code is needed at least in the following case:
  1138.          The user types "next" and then a signal arrives (before
  1139.          the "next" is done).  */
  1140.           /* We've just taken a signal; go until we are back to
  1141.          the point where we took it and one more.  */
  1142.           step_resume_break_address = prev_pc;
  1143.           step_resume_break_duplicate =
  1144.         breakpoint_here_p (step_resume_break_address);
  1145.           if (breakpoints_inserted)
  1146.         insert_step_breakpoint ();
  1147.           /* Make sure that the stepping range gets us past
  1148.          that instruction.  */
  1149.           if (step_range_end == 1)
  1150.         step_range_end = (step_range_start = prev_pc) + 1;
  1151.           remove_breakpoints_on_following_step = 1;
  1152.         }
  1153.  
  1154.       /* ==> See comments at top of file on this algorithm.  <==*/
  1155.       
  1156.       else if (stop_pc == stop_func_start
  1157.           && (stop_func_start != prev_func_start
  1158.           || prologue_pc != stop_func_start
  1159.           || stop_sp != prev_sp))
  1160.         {
  1161.           /* It's a subroutine call */
  1162.           if (step_over_calls > 0 
  1163.           || (step_over_calls &&  find_pc_function (stop_pc) == 0))
  1164.         {
  1165.           /* A subroutine call has happened.  */
  1166.           /* Set a special breakpoint after the return */
  1167.           step_resume_break_address =
  1168.             ADDR_BITS_REMOVE
  1169.               (SAVED_PC_AFTER_CALL (get_current_frame ()));
  1170.           step_resume_break_duplicate
  1171.             = breakpoint_here_p (step_resume_break_address);
  1172.           if (breakpoints_inserted)
  1173.             insert_step_breakpoint ();
  1174.         }
  1175.           /* Subroutine call with source code we should not step over.
  1176.          Do step to the first line of code in it.  */
  1177.           else if (step_over_calls)
  1178.         {
  1179.           SKIP_PROLOGUE (stop_func_start);
  1180.           sal = find_pc_line (stop_func_start, 0);
  1181.           /* Use the step_resume_break to step until
  1182.              the end of the prologue, even if that involves jumps
  1183.              (as it seems to on the vax under 4.2).  */
  1184.           /* If the prologue ends in the middle of a source line,
  1185.              continue to the end of that source line.
  1186.              Otherwise, just go to end of prologue.  */
  1187. #ifdef PROLOGUE_FIRSTLINE_OVERLAP
  1188.           /* no, don't either.  It skips any code that's
  1189.              legitimately on the first line.  */
  1190. #else
  1191.           if (sal.end && sal.pc != stop_func_start)
  1192.             stop_func_start = sal.end;
  1193. #endif
  1194.           
  1195.           if (stop_func_start == stop_pc)
  1196.             {
  1197.               /* We are already there: stop now.  */
  1198.               stop_step = 1;
  1199.               break;
  1200.             }
  1201.           else
  1202.             /* Put the step-breakpoint there and go until there. */
  1203.             {
  1204.               step_resume_break_address = stop_func_start;
  1205.               
  1206.               step_resume_break_duplicate
  1207.             = breakpoint_here_p (step_resume_break_address);
  1208.               if (breakpoints_inserted)
  1209.             insert_step_breakpoint ();
  1210.               /* Do not specify what the fp should be when we stop
  1211.              since on some machines the prologue
  1212.              is where the new fp value is established.  */
  1213.               step_frame_address = 0;
  1214.               /* And make sure stepping stops right away then.  */
  1215.               step_range_end = step_range_start;
  1216.             }
  1217.         }
  1218.           else
  1219.         {
  1220.           /* We get here only if step_over_calls is 0 and we
  1221.              just stepped into a subroutine.  I presume
  1222.              that step_over_calls is only 0 when we're
  1223.              supposed to be stepping at the assembly
  1224.              language level.*/
  1225.           stop_step = 1;
  1226.           break;
  1227.         }
  1228.         }
  1229.       /* No subroutince call; stop now.  */
  1230.       else
  1231.         {
  1232.           stop_step = 1;
  1233.           break;
  1234.         }
  1235.     }
  1236.  
  1237.       else if (trap_expected
  1238.            && IN_SIGTRAMP (stop_pc, stop_func_name)
  1239.            && !IN_SIGTRAMP (prev_pc, prev_func_name))
  1240.     {
  1241.       /* What has happened here is that we have just stepped the inferior
  1242.          with a signal (because it is a signal which shouldn't make
  1243.          us stop), thus stepping into sigtramp.
  1244.  
  1245.          So we need to set a step_resume_break_address breakpoint
  1246.          and continue until we hit it, and then step.  */
  1247.       step_resume_break_address = prev_pc;
  1248.       /* Always 1, I think, but it's probably easier to have
  1249.          the step_resume_break as usual rather than trying to
  1250.          re-use the breakpoint which is already there.  */
  1251.       step_resume_break_duplicate =
  1252.         breakpoint_here_p (step_resume_break_address);
  1253.       if (breakpoints_inserted)
  1254.         insert_step_breakpoint ();
  1255.       remove_breakpoints_on_following_step = 1;
  1256.       another_trap = 1;
  1257.     }
  1258.  
  1259.       /* Save the pc before execution, to compare with pc after stop.  */
  1260.       prev_pc = read_pc ();    /* Might have been DECR_AFTER_BREAK */
  1261.       prev_func_start = stop_func_start; /* Ok, since if DECR_PC_AFTER
  1262.                       BREAK is defined, the
  1263.                       original pc would not have
  1264.                       been at the start of a
  1265.                       function. */
  1266.       prev_func_name = stop_func_name;
  1267.       prev_sp = stop_sp;
  1268.  
  1269.       /* If we did not do break;, it means we should keep
  1270.      running the inferior and not return to debugger.  */
  1271.  
  1272.       if (trap_expected && stop_signal != SIGTRAP)
  1273.     {
  1274.       /* We took a signal (which we are supposed to pass through to
  1275.          the inferior, else we'd have done a break above) and we
  1276.          haven't yet gotten our trap.  Simply continue.  */
  1277.       resume ((step_range_end && !step_resume_break_address)
  1278.           || (trap_expected && !step_resume_break_address)
  1279.           || bpstat_should_step (),
  1280.           stop_signal);
  1281.     }
  1282.       else
  1283.     {
  1284.       /* Either the trap was not expected, but we are continuing
  1285.          anyway (the user asked that this signal be passed to the
  1286.          child)
  1287.            -- or --
  1288.          The signal was SIGTRAP, e.g. it was our signal, but we
  1289.          decided we should resume from it.
  1290.  
  1291.          We're going to run this baby now!
  1292.  
  1293.          Insert breakpoints now, unless we are trying
  1294.          to one-proceed past a breakpoint.  */
  1295.       /* If we've just finished a special step resume and we don't
  1296.          want to hit a breakpoint, pull em out.  */
  1297. #ifdef TDESC
  1298.           if (!tdesc_handle)
  1299.             {
  1300.           init_tdesc();
  1301.               safe_to_init_tdesc_context = 1;
  1302.             }
  1303. #endif
  1304.  
  1305.       if (!step_resume_break_address &&
  1306.           remove_breakpoints_on_following_step)
  1307.         {
  1308.           remove_breakpoints_on_following_step = 0;
  1309.           remove_breakpoints ();
  1310.           breakpoints_inserted = 0;
  1311.         }
  1312.       else if (!breakpoints_inserted &&
  1313.            (step_resume_break_address != NULL || !another_trap))
  1314.         {
  1315.           insert_step_breakpoint ();
  1316.           breakpoints_failed = insert_breakpoints ();
  1317.           if (breakpoints_failed)
  1318.         break;
  1319.           breakpoints_inserted = 1;
  1320.         }
  1321.  
  1322.       trap_expected = another_trap;
  1323.  
  1324.       if (stop_signal == SIGTRAP)
  1325.         stop_signal = 0;
  1326.  
  1327. #ifdef SHIFT_INST_REGS
  1328.       /* I'm not sure when this following segment applies.  I do know, now,
  1329.          that we shouldn't rewrite the regs when we were stopped by a
  1330.          random signal from the inferior process.  */
  1331.  
  1332.           if (!bpstat_explains_signal (stop_bpstat)
  1333.           && (stop_signal != SIGCLD) 
  1334.               && !stopped_by_random_signal)
  1335.             {
  1336.             CORE_ADDR pc_contents = read_register (PC_REGNUM);
  1337.             CORE_ADDR npc_contents = read_register (NPC_REGNUM);
  1338.             if (pc_contents != npc_contents)
  1339.               {
  1340.               write_register (NNPC_REGNUM, npc_contents);
  1341.               write_register (NPC_REGNUM, pc_contents);
  1342.           }
  1343.             }
  1344. #endif /* SHIFT_INST_REGS */
  1345.  
  1346.       resume ((step_range_end && !step_resume_break_address)
  1347.           || (trap_expected && !step_resume_break_address)
  1348.           || bpstat_should_step (),
  1349.           stop_signal);
  1350.     }
  1351.     }
  1352.   if (target_has_execution)
  1353.     {
  1354.       /* Assuming the inferior still exists, set these up for next
  1355.      time, just like we did above if we didn't break out of the
  1356.      loop.  */
  1357.       prev_pc = read_pc ();
  1358.       prev_func_start = stop_func_start;
  1359.       prev_func_name = stop_func_name;
  1360.       prev_sp = stop_sp;
  1361.     }
  1362. }
  1363.  
  1364. /* Here to return control to GDB when the inferior stops for real.
  1365.    Print appropriate messages, remove breakpoints, give terminal our modes.
  1366.  
  1367.    STOP_PRINT_FRAME nonzero means print the executing frame
  1368.    (pc, function, args, file, line number and line text).
  1369.    BREAKPOINTS_FAILED nonzero means stop was due to error
  1370.    attempting to insert breakpoints.  */
  1371.  
  1372. void
  1373. normal_stop ()
  1374. {
  1375.   /* Make sure that the current_frame's pc is correct.  This
  1376.      is a correction for setting up the frame info before doing
  1377.      DECR_PC_AFTER_BREAK */
  1378.   if (target_has_execution)
  1379.     (get_current_frame ())->pc = read_pc ();
  1380.   
  1381.   if (breakpoints_failed)
  1382.     {
  1383.       target_terminal_ours_for_output ();
  1384.       print_sys_errmsg ("ptrace", breakpoints_failed);
  1385.       printf ("Stopped; cannot insert breakpoints.\n\
  1386. The same program may be running in another process.\n");
  1387.     }
  1388.  
  1389.   if (target_has_execution)
  1390.     remove_step_breakpoint ();
  1391.  
  1392.   if (target_has_execution && breakpoints_inserted)
  1393.     if (remove_breakpoints ())
  1394.       {
  1395.     target_terminal_ours_for_output ();
  1396.     printf ("Cannot remove breakpoints because program is no longer writable.\n\
  1397. It might be running in another process.\n\
  1398. Further execution is probably impossible.\n");
  1399.       }
  1400.  
  1401.   breakpoints_inserted = 0;
  1402.  
  1403.   /* Delete the breakpoint we stopped at, if it wants to be deleted.
  1404.      Delete any breakpoint that is to be deleted at the next stop.  */
  1405.  
  1406.   breakpoint_auto_delete (stop_bpstat);
  1407.  
  1408.   /* If an auto-display called a function and that got a signal,
  1409.      delete that auto-display to avoid an infinite recursion.  */
  1410.  
  1411.   if (stopped_by_random_signal)
  1412.     disable_current_display ();
  1413.  
  1414.   if (step_multi && stop_step)
  1415.     return;
  1416.  
  1417.   target_terminal_ours ();
  1418.  
  1419.   if (!target_has_stack)
  1420.     return;
  1421.  
  1422.   /* Select innermost stack frame except on return from a stack dummy routine,
  1423.      or if the program has exited.  */
  1424.   if (!stop_stack_dummy)
  1425.     {
  1426.       select_frame (get_current_frame (), 0);
  1427.  
  1428.       if (stop_print_frame)
  1429.     {
  1430.       int source_only = bpstat_print (stop_bpstat);
  1431.       print_sel_frame
  1432.         (source_only
  1433.          || (stop_step
  1434.          && step_frame_address == stop_frame_address
  1435.          && step_start_function == find_pc_function (stop_pc)));
  1436.  
  1437.       /* Display the auto-display expressions.  */
  1438.       do_displays ();
  1439.     }
  1440.     }
  1441.  
  1442.   /* Save the function value return registers, if we care.
  1443.      We might be about to restore their previous contents.  */
  1444.   if (proceed_to_finish)
  1445.     read_register_bytes (0, stop_registers, REGISTER_BYTES);
  1446.  
  1447.   if (stop_stack_dummy)
  1448.     {
  1449.       /* Pop the empty frame that contains the stack dummy.
  1450.          POP_FRAME ends with a setting of the current frame, so we
  1451.      can use that next. */
  1452.       POP_FRAME;
  1453.       select_frame (get_current_frame (), 0);
  1454.     }
  1455. }
  1456.  
  1457. static void
  1458. insert_step_breakpoint ()
  1459. {
  1460.   if (step_resume_break_address && !step_resume_break_duplicate)
  1461.     target_insert_breakpoint (step_resume_break_address,
  1462.                   step_resume_break_shadow);
  1463. }
  1464.  
  1465. static void
  1466. remove_step_breakpoint ()
  1467. {
  1468.   if (step_resume_break_address && !step_resume_break_duplicate)
  1469.     target_remove_breakpoint (step_resume_break_address,
  1470.                   step_resume_break_shadow);
  1471. }
  1472.  
  1473. static void
  1474. sig_print_header ()
  1475. {
  1476.   printf_filtered ("Signal\t\tStop\tPrint\tPass to program\tDescription\n");
  1477. }
  1478.  
  1479. static void
  1480. sig_print_info (number)
  1481.      int number;
  1482. {
  1483.   char *abbrev = sig_abbrev(number);
  1484.   if (abbrev == NULL)
  1485.     printf_filtered ("%d\t\t", number);
  1486.   else
  1487.     printf_filtered ("SIG%s (%d)\t", abbrev, number);
  1488.   printf_filtered ("%s\t", signal_stop[number] ? "Yes" : "No");
  1489.   printf_filtered ("%s\t", signal_print[number] ? "Yes" : "No");
  1490.   printf_filtered ("%s\t\t", signal_program[number] ? "Yes" : "No");
  1491.   printf_filtered ("%s\n", sys_siglist[number]);
  1492. }
  1493.  
  1494. /* Specify how various signals in the inferior should be handled.  */
  1495.  
  1496. static void
  1497. handle_command (args, from_tty)
  1498.      char *args;
  1499.      int from_tty;
  1500. {
  1501.   register char *p = args;
  1502.   int signum = 0;
  1503.   register int digits, wordlen;
  1504.   char *nextarg;
  1505.  
  1506.   if (!args)
  1507.     error_no_arg ("signal to handle");
  1508.  
  1509.   while (*p)
  1510.     {
  1511.       /* Find the end of the next word in the args.  */
  1512.       for (wordlen = 0;
  1513.        p[wordlen] && p[wordlen] != ' ' && p[wordlen] != '\t';
  1514.        wordlen++);
  1515.       /* Set nextarg to the start of the word after the one we just
  1516.      found, and null-terminate this one.  */
  1517.       if (p[wordlen] == '\0')
  1518.     nextarg = p + wordlen;
  1519.       else
  1520.     {
  1521.       p[wordlen] = '\0';
  1522.       nextarg = p + wordlen + 1;
  1523.     }
  1524.       
  1525.  
  1526.       for (digits = 0; p[digits] >= '0' && p[digits] <= '9'; digits++);
  1527.  
  1528.       if (signum == 0)
  1529.     {
  1530.       /* It is the first argument--must be the signal to operate on.  */
  1531.       if (digits == wordlen)
  1532.         {
  1533.           /* Numeric.  */
  1534.           signum = atoi (p);
  1535.           if (signum <= 0 || signum >= NSIG)
  1536.         {
  1537.           p[wordlen] = '\0';
  1538.           error ("Invalid signal %s given as argument to \"handle\".", p);
  1539.         }
  1540.         }
  1541.       else
  1542.         {
  1543.           /* Symbolic.  */
  1544.           signum = sig_number (p);
  1545.           if (signum == -1)
  1546.         error ("No such signal \"%s\"", p);
  1547.         }
  1548.  
  1549.       if (signum == SIGTRAP || signum == SIGINT)
  1550.         {
  1551.           if (!query ("SIG%s is used by the debugger.\nAre you sure you want to change it? ", sig_abbrev (signum)))
  1552.         error ("Not confirmed.");
  1553.         }
  1554.     }
  1555.       /* Else, if already got a signal number, look for flag words
  1556.      saying what to do for it.  */
  1557.       else if (!strncmp (p, "stop", wordlen))
  1558.     {
  1559.       signal_stop[signum] = 1;
  1560.       signal_print[signum] = 1;
  1561.     }
  1562.       else if (wordlen >= 2 && !strncmp (p, "print", wordlen))
  1563.     signal_print[signum] = 1;
  1564.       else if (wordlen >= 2 && !strncmp (p, "pass", wordlen))
  1565.     signal_program[signum] = 1;
  1566.       else if (!strncmp (p, "ignore", wordlen))
  1567.     signal_program[signum] = 0;
  1568.       else if (wordlen >= 3 && !strncmp (p, "nostop", wordlen))
  1569.     signal_stop[signum] = 0;
  1570.       else if (wordlen >= 4 && !strncmp (p, "noprint", wordlen))
  1571.     {
  1572.       signal_print[signum] = 0;
  1573.       signal_stop[signum] = 0;
  1574.     }
  1575.       else if (wordlen >= 4 && !strncmp (p, "nopass", wordlen))
  1576.     signal_program[signum] = 0;
  1577.       else if (wordlen >= 3 && !strncmp (p, "noignore", wordlen))
  1578.     signal_program[signum] = 1;
  1579.       /* Not a number and not a recognized flag word => complain.  */
  1580.       else
  1581.     {
  1582.       error ("Unrecognized flag word: \"%s\".", p);
  1583.     }
  1584.  
  1585.       /* Find start of next word.  */
  1586.       p = nextarg;
  1587.       while (*p == ' ' || *p == '\t') p++;
  1588.     }
  1589.  
  1590.   if (from_tty)
  1591.     {
  1592.       /* Show the results.  */
  1593.       sig_print_header ();
  1594.       sig_print_info (signum);
  1595.     }
  1596. }
  1597.  
  1598. /* Print current contents of the tables set by the handle command.  */
  1599.  
  1600. static void
  1601. signals_info (signum_exp)
  1602.      char *signum_exp;
  1603. {
  1604.   register int i;
  1605.   sig_print_header ();
  1606.  
  1607.   if (signum_exp)
  1608.     {
  1609.       /* First see if this is a symbol name.  */
  1610.       i = sig_number (signum_exp);
  1611.       if (i == -1)
  1612.     {
  1613.       /* Nope, maybe it's an address which evaluates to a signal
  1614.          number.  */
  1615.       i = parse_and_eval_address (signum_exp);
  1616.       if (i >= NSIG || i < 0)
  1617.         error ("Signal number out of bounds.");
  1618.     }
  1619.       sig_print_info (i);
  1620.       return;
  1621.     }
  1622.  
  1623.   printf_filtered ("\n");
  1624.   for (i = 0; i < NSIG; i++)
  1625.     {
  1626.       QUIT;
  1627.  
  1628.       sig_print_info (i);
  1629.     }
  1630.  
  1631.   printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
  1632. }
  1633.  
  1634. /* Save all of the information associated with the inferior<==>gdb
  1635.    connection.  INF_STATUS is a pointer to a "struct inferior_status"
  1636.    (defined in inferior.h).  */
  1637.  
  1638. void
  1639. save_inferior_status (inf_status, restore_stack_info)
  1640.      struct inferior_status *inf_status;
  1641.      int restore_stack_info;
  1642. {
  1643.   inf_status->pc_changed = pc_changed;
  1644.   inf_status->stop_signal = stop_signal;
  1645.   inf_status->stop_pc = stop_pc;
  1646.   inf_status->stop_frame_address = stop_frame_address;
  1647.   inf_status->stop_step = stop_step;
  1648.   inf_status->stop_stack_dummy = stop_stack_dummy;
  1649.   inf_status->stopped_by_random_signal = stopped_by_random_signal;
  1650.   inf_status->trap_expected = trap_expected;
  1651.   inf_status->step_range_start = step_range_start;
  1652.   inf_status->step_range_end = step_range_end;
  1653.   inf_status->step_frame_address = step_frame_address;
  1654.   inf_status->step_over_calls = step_over_calls;
  1655.   inf_status->step_resume_break_address = step_resume_break_address;
  1656.   inf_status->stop_after_trap = stop_after_trap;
  1657.   inf_status->stop_soon_quietly = stop_soon_quietly;
  1658.   /* Save original bpstat chain here; replace it with copy of chain. 
  1659.      If caller's caller is walking the chain, they'll be happier if we
  1660.      hand them back the original chain when restore_i_s is called.  */
  1661.   inf_status->stop_bpstat = stop_bpstat;
  1662.   stop_bpstat = bpstat_copy (stop_bpstat);
  1663.   inf_status->breakpoint_proceeded = breakpoint_proceeded;
  1664.   inf_status->restore_stack_info = restore_stack_info;
  1665.   inf_status->proceed_to_finish = proceed_to_finish;
  1666.   
  1667.   bcopy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
  1668.   
  1669.   record_selected_frame (&(inf_status->selected_frame_address),
  1670.              &(inf_status->selected_level));
  1671.   return;
  1672. }
  1673.  
  1674. void
  1675. restore_inferior_status (inf_status)
  1676.      struct inferior_status *inf_status;
  1677. {
  1678.   FRAME fid;
  1679.   int level = inf_status->selected_level;
  1680.  
  1681.   pc_changed = inf_status->pc_changed;
  1682.   stop_signal = inf_status->stop_signal;
  1683.   stop_pc = inf_status->stop_pc;
  1684.   stop_frame_address = inf_status->stop_frame_address;
  1685.   stop_step = inf_status->stop_step;
  1686.   stop_stack_dummy = inf_status->stop_stack_dummy;
  1687.   stopped_by_random_signal = inf_status->stopped_by_random_signal;
  1688.   trap_expected = inf_status->trap_expected;
  1689.   step_range_start = inf_status->step_range_start;
  1690.   step_range_end = inf_status->step_range_end;
  1691.   step_frame_address = inf_status->step_frame_address;
  1692.   step_over_calls = inf_status->step_over_calls;
  1693.   step_resume_break_address = inf_status->step_resume_break_address;
  1694.   stop_after_trap = inf_status->stop_after_trap;
  1695.   stop_soon_quietly = inf_status->stop_soon_quietly;
  1696.   bpstat_clear (&stop_bpstat);
  1697.   stop_bpstat = inf_status->stop_bpstat;
  1698.   breakpoint_proceeded = inf_status->breakpoint_proceeded;
  1699.   proceed_to_finish = inf_status->proceed_to_finish;
  1700.  
  1701.   bcopy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
  1702.  
  1703.   /* The inferior can be gone if the user types "print exit(0)"
  1704.      (and perhaps other times).  */
  1705.   if (target_has_stack && inf_status->restore_stack_info)
  1706.     {
  1707.       fid = find_relative_frame (get_current_frame (),
  1708.                  &level);
  1709.  
  1710.       /* If inf_status->selected_frame_address is NULL, there was no
  1711.      previously selected frame.  */
  1712.       if (fid == 0 ||
  1713.       FRAME_FP (fid) != inf_status->selected_frame_address ||
  1714.       level != 0)
  1715.     {
  1716. #if 0
  1717.       /* I'm not sure this error message is a good idea.  I have
  1718.          only seen it occur after "Can't continue previously
  1719.          requested operation" (we get called from do_cleanups), in
  1720.          which case it just adds insult to injury (one confusing
  1721.          error message after another.  Besides which, does the
  1722.          user really care if we can't restore the previously
  1723.          selected frame?  */
  1724.       fprintf (stderr, "Unable to restore previously selected frame.\n");
  1725. #endif
  1726.       select_frame (get_current_frame (), 0);
  1727.       return;
  1728.     }
  1729.       
  1730.       select_frame (fid, inf_status->selected_level);
  1731.     }
  1732. }
  1733.  
  1734.  
  1735. void
  1736. _initialize_infrun ()
  1737. {
  1738.   register int i;
  1739.  
  1740.   add_info ("signals", signals_info,
  1741.         "What debugger does when program gets various signals.\n\
  1742. Specify a signal number as argument to print info on that signal only.");
  1743.  
  1744.   add_com ("handle", class_run, handle_command,
  1745.        "Specify how to handle a signal.\n\
  1746. Args are signal number followed by flags.\n\
  1747. Flags allowed are \"stop\", \"print\", \"pass\",\n\
  1748.  \"nostop\", \"noprint\" or \"nopass\".\n\
  1749. Print means print a message if this signal happens.\n\
  1750. Stop means reenter debugger if this signal happens (implies print).\n\
  1751. Pass means let program see this signal; otherwise program doesn't know.\n\
  1752. Pass and Stop may be combined.");
  1753.  
  1754.   for (i = 0; i < NSIG; i++)
  1755.     {
  1756.       signal_stop[i] = 1;
  1757.       signal_print[i] = 1;
  1758.       signal_program[i] = 1;
  1759.     }
  1760.  
  1761.   /* Signals caused by debugger's own actions
  1762.      should not be given to the program afterwards.  */
  1763.   signal_program[SIGTRAP] = 0;
  1764.   signal_program[SIGINT] = 0;
  1765.  
  1766.   /* Signals that are not errors should not normally enter the debugger.  */
  1767. #ifdef SIGALRM
  1768.   signal_stop[SIGALRM] = 0;
  1769.   signal_print[SIGALRM] = 0;
  1770. #endif /* SIGALRM */
  1771. #ifdef SIGVTALRM
  1772.   signal_stop[SIGVTALRM] = 0;
  1773.   signal_print[SIGVTALRM] = 0;
  1774. #endif /* SIGVTALRM */
  1775. #ifdef SIGPROF
  1776.   signal_stop[SIGPROF] = 0;
  1777.   signal_print[SIGPROF] = 0;
  1778. #endif /* SIGPROF */
  1779. #ifdef SIGCHLD
  1780.   signal_stop[SIGCHLD] = 0;
  1781.   signal_print[SIGCHLD] = 0;
  1782. #endif /* SIGCHLD */
  1783. #ifdef SIGCLD
  1784.   signal_stop[SIGCLD] = 0;
  1785.   signal_print[SIGCLD] = 0;
  1786. #endif /* SIGCLD */
  1787. #ifdef SIGIO
  1788.   signal_stop[SIGIO] = 0;
  1789.   signal_print[SIGIO] = 0;
  1790. #endif /* SIGIO */
  1791. #ifdef SIGURG
  1792.   signal_stop[SIGURG] = 0;
  1793.   signal_print[SIGURG] = 0;
  1794. #endif /* SIGURG */
  1795. }
  1796.  
  1797.